package com.bw.lib_mvvm

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Looper
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding


/**
 *@ClassName BaseActivity
 *@Author hu..
 *@Data 2024/7/21
 *@Description
 */
abstract class BaseActivity <VDB: ViewDataBinding>: AppCompatActivity(){
    protected lateinit var binding: VDB

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this,bindLayout())
        hide()
        initView()
        initData()
        handleObserver()
    }
    abstract fun initView()
    abstract fun initData()
    abstract fun bindLayout(): Int
    open fun handleObserver(){
    }

    fun hide(){
        val decorView = window.decorView
        var option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        decorView.systemUiVisibility = option
        window.statusBarColor = Color.TRANSPARENT
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus && Build.VERSION.SDK_INT >= 19) {
            val decorView = window.decorView
            decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
        }
    }

    fun showMsg(msg:String){
        if(Looper.getMainLooper().thread != Thread.currentThread()){
            runOnUiThread{
                showMsgReal(msg)
            }
        }else{
            showMsgReal(msg)
        }
    }

    private fun showMsgReal(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
    }
}